package com.chookss.home.other;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.StaticClass;
import com.chookss.base.X5WebView;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lvgroup.hospital.base.BaseAct;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WxBindActivity extends BaseAct {
    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.rightTv)
    TextView rightTv;
    @BindView(R.id.leftLl)
    LinearLayout leftLl;
    @BindView(R.id.lineBottom)
    View lineBottom;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    @BindView(R.id.webView)
    X5WebView webView;
    private String currentDate;
    private boolean isBind = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx_bind);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState3();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        currentDate = sf.format(c.getTime());
        commonTitleTxt.setText(getIntent().getStringExtra("title"));
        if (new ShareUtils().getBoolean(this, StaticClass.BIND_WX, false)) {
         //已经绑定过了
            llBottom.setVisibility(View.GONE);
        }else{
            llBottom.setVisibility(View.VISIBLE);
            leftLl.setVisibility(View.VISIBLE);
        }
        showLoading();
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                try {
                    dismissLoading();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        webView.loadUrl(getIntent().getStringExtra("url"));
    }

    private void getWxDevice() {
        MyHttpRequest.getDefault().postRequestCompat(null, Urls.selectWxDevice, SignUtils2.makeSignStr(null), false, new ObserverCallback<String>() {
            @Override
            public void onSuccess(String data) {
                try {
                    JSONObject jsonObject = new JSONObject(data);
                    if ("200".equals(jsonObject.getString("code"))) {
                        String device = jsonObject.getString("data");
                        MyToast.show("已复制到剪切板，请去微信中绑定");
                        ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboardManager.setPrimaryClip(ClipData.newPlainText("", device));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getMap() != null) {
            Map<String, String> map = messageEvent.getMap();

        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if (msg.equals("Web_Close")) {
                finish();
            }else if (msg.equals("Web_Cancel_dialog")) {
                dismissLoading();
            }
        }
    }
    @OnClick({R.id.common_title_back, R.id.leftLl, R.id.rightTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.rightTv:
                getWxDevice();
                break;
            case R.id.leftLl:
                MyToast.show("已设置近期不再提醒");
                new ShareUtils().putLong(this, StaticClass.EMPLOYEECODE+"-"+StaticClass.BIND_WX_TIME, System.currentTimeMillis());
                break;
        }
    }
}
